﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

using Actuant.MOSS.TimeJob;

namespace Actuant.MOSS.Feature.FeatureReceiver
{
    public class SMSTaskJobReceiver : SPFeatureReceiver
    {
        const string JobName = "SMSTaskJob";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (var job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JobName)
                {
                    job.Delete();
                }
            }

            SMSTaskJob myTask = new SMSTaskJob(JobName, site.WebApplication);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;

            myTask.Schedule = schedule;
            myTask.Update();

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            
            // delete the job
            foreach (var job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JobName)
                {
                    job.Delete();
                }
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            // throw new NotImplementedException();
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            // throw new NotImplementedException();
        }
    }
}
